#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zookeeper/zookeeper.h>
#include "global.h"
#include "logger.h"

#define ZKPATH_ROOT          "/conf_htiiot"
#define ZKPATH_IPTRAFIC      ZKPATH_ROOT "/myiptrafic"
#define ZKPATH_REDIS         ZKPATH_ROOT "/redis"

/*******************************************************************************
 * [in]  phZookeeper - success handle built to zookeeper                       *
 * [in]  pFullName   - full path name of your param name, e.g: /ur/path/name   *
 * [ret]               >0: length of the value other:failure                   *
*******************************************************************************/
/*
static int GetZookeeperStringValLen(zhandle_t* phZookeeper, const char* pFullName)
{
    struct Stat stat = { 0 };

    if(!phZookeeper || !pFullName || !*pFullName)
        return -1;

    if(ZOK == zoo_exists(phZookeeper, pFullName, 0, &stat))
        return stat.dataLength;

    return -1;
}
*/
/*******************************************************************************
 * [in]  phZookeeper - success handle built to zookeeper                       *
 * [in]  pFullName   - full path name of your param name, e.g: /ur/path/name   *
 * [out] pValue      - value buffer                                            *
 * [in]  nLen        - size of buffer                                          *
 * [ret]               null:failure  other:pValue                              *
*******************************************************************************/
static const char* GetZookeeperStringVal(zhandle_t*  phZookeeper,
                                         const char* pFullName,
                                         char*       pValue,
                                         int         nLen)
{
    if(!phZookeeper || !pFullName || !*pFullName || !pValue || !nLen)
        return NULL;

    if(ZOK == zoo_get(phZookeeper, pFullName, 0, pValue, &nLen, NULL))
        return pValue;

    return NULL;
}
/*******************************************************************************
 * [in]  phZookeeper - success handle built to zookeeper                       *
 * [in]  pFullName   - full path name of your param name, e.g: /ur/path/name   *
 * [out] pErrCode    - not null will be signed an error code                   *
 * [ret]               integer value of the parameter                          *
*******************************************************************************/
/*
static int GetZookeeperIntVal(zhandle_t*  phZookeeper,
                              const char* pFullName)
{
    if(!phZookeeper || !pFullName || !*pFullName)
        return -1;

    char szValue[32] = { 0 };
    if(GetZookeeperStringVal(phZookeeper, pFullName, szValue, (int)sizeof(szValue)))
        return atoi(szValue);

    return -1;
}
*/

int InitZkProfile()
{
    int        i           = 0;
    int        nMaxRetry   = 10;
    int        nTimeout    = 30000; /* milliseconds, 30 seconds here */
    zhandle_t* phZookeeper = NULL;

    zoo_set_debug_level(ZOO_LOG_LEVEL_WARN);

    for(; i<nMaxRetry; ++i)
    {
        phZookeeper = zookeeper_init(g_pZookeeper,
                                     NULL,
                                     nTimeout,
                                     0,
                                     "hello zookeeper.",
                                     0);

        if (phZookeeper)
            break;

        if(i < nMaxRetry-1)
        {
            LOG__WARN("Failed to build connection with zookeeper, retrying ...");
            sleep(1);
        }
    }

    if(i == nMaxRetry)
    {
        LOG__ERROR("Failed to build connection with zookeeper, exiting...");
        return -1;
    }

    LOG__INFO("init zookeeper successful");


    if(!*g_htProfile.m_szMqttFilterRule)
        GetZookeeperStringVal(phZookeeper, ZKPATH_IPTRAFIC"/mqttfilter", g_htProfile.m_szMqttFilterRule, sizeof(g_htProfile.m_szMqttFilterRule));

    if(!*g_htProfile.m_szRestFilterRule)
        GetZookeeperStringVal(phZookeeper, ZKPATH_IPTRAFIC"/restfilter", g_htProfile.m_szRestFilterRule, sizeof(g_htProfile.m_szRestFilterRule));

    if(!*g_htProfile.m_szTzFilterRule)
        GetZookeeperStringVal(phZookeeper, ZKPATH_IPTRAFIC"/tzfilter", g_htProfile.m_szTzFilterRule, sizeof(g_htProfile.m_szTzFilterRule));

    if(!*g_htProfile.m_szRedisIp)
        GetZookeeperStringVal(phZookeeper, ZKPATH_REDIS"/address", g_htProfile.m_szRedisIp, sizeof(g_htProfile.m_szRedisIp));

    if(!*g_htProfile.m_szRedisPort)
        GetZookeeperStringVal(phZookeeper, ZKPATH_REDIS"/port", g_htProfile.m_szRedisPort, sizeof(g_htProfile.m_szRedisPort));

    if(!*g_htProfile.m_szRedisAuth)
        GetZookeeperStringVal(phZookeeper, ZKPATH_REDIS"/auth", g_htProfile.m_szRedisAuth, sizeof(g_htProfile.m_szRedisAuth));


    zookeeper_close(phZookeeper);


    LOG__INFO("param:zookeeper        			= %s",  g_pZookeeper);
    LOG__INFO("param:m_bMqttCommand   			= %d",  g_htProfile.m_bMqttCommand);
    LOG__INFO("param:m_bRestCommand   			= %d",  g_htProfile.m_bRestCommand);
    LOG__INFO("param:m_bTzCommand     			= %d",  g_htProfile.m_bTzCommand);
    LOG__INFO("param:mqtt.m_szMqttFilterRule    = %s",  g_htProfile.m_szMqttFilterRule);
    LOG__INFO("param:mqtt.m_szRestFilterRule    = %s",  g_htProfile.m_szRestFilterRule);
    LOG__INFO("param:mqtt.m_szTzFilterRule      = %s",  g_htProfile.m_szTzFilterRule);
    LOG__INFO("param:mqtt.m_szRedisIp       	= %s",  g_htProfile.m_szRedisIp);
    LOG__INFO("param:mqtt.m_szRedisPort         = %s",  g_htProfile.m_szRedisPort);
    LOG__INFO("param:m_szRedisAuth         		= %s",  g_htProfile.m_szRedisAuth);

    return 0;
}

